﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MagicFight
{
    public class Evocation : Magic
    {
        int pointGain = Def.EvocationDef.PointGain;
        int manaGain = Def.EvocationDef.ManaGain;
        int gainInterval = Def.EvocationDef.GainInterval;

        double timer = 0;
        int casterPrevHP = 0;

        public Evocation(Character caster)
            : base(caster, Def.EvocationDef.N)
        {
            casterPrevHP = caster.HP;
            foreach (Magic m in ps.magics)
                if (m is Evocation && m.caster == this.caster) { (m as Evocation).Die(); this.Die(); }
        }

        public override void Update(GameTime gameTime)
        {
            if (!alive) return;
            if (!caster.alive) { alive = false; return; }
            if (caster.isCasting)
            {
                if (caster.countered) { Die(); return; }
                caster.castTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (caster.castTimer > caster.castTime) Die();
            }
            Effect(gameTime);
        }

        public override void Effect(GameTime gameTime)
        {
            if (casterPrevHP > caster.HP) { Die(); return; }
            casterPrevHP = caster.HP;
            timer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (timer % 50 == 0) ps.effectsManager.Add(new DrainAura(caster.position));
            if (timer > gainInterval)
            {
                timer -= gainInterval;
                
                int manaGained = caster.GainMP(manaGain);
                caster.AddPoints(MagicType.Arcane, pointGain);
                ps.effectsManager.Add(new FloatingText(caster.position, "+" + manaGained + " MP", Color.Blue));
            }
        }

        public void Die()
        {
            caster.castTimer = 1;
            caster.isCasting = false;
            caster.countered = false;
            alive = false;
        }
    }
}
